<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <link rel="stylesheet" href="/layui/css/layui.css">
    <script src="/layui/layui.js"></script>
    <style>
        body {
            background: #FCFCFC;
        }
    </style>
</head>
<body>
    <div class="layui-fluid" style="margin-top: 10px;">
        <span class="layui-breadcrumb">
            <a href="">财务管理</a>
            <a href="">支出管理</a>
            <a><cite>列表</cite></a>
        </span>
    </div>
    <form class="layui-form" action="" style="margin-top: 20px;">
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">支出类型</label>
                <div class="layui-input-inline" style="width: 100px;">
                <input type="text" name="expenseType" id="expenseType" placeholder="支出类型" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">支出范围</label>
                <div class="layui-input-inline" style="width: 100px;">
                    <input type="text" name="price_min" id="price_min" placeholder="￥" autocomplete="off" class="layui-input">
                </div>
                <div class="layui-form-mid">-</div>
                <div class="layui-input-inline" style="width: 100px;">
                    <input type="text" name="price_max" id="price_max" placeholder="￥" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <button type="button" class="layui-btn" id="search">查 询</button>
            </div>
            <div class="layui-inline" style="float: right; padding-right: 35px;">
                <button type="button" class="layui-btn" id="add">添 加</button>
            </div>
        </div>
    </form>
    <div style="padding-left: 40px; padding-right: 40px;">
        <table id="tbl_zhichu" lay-filter="table_tool" class="layui-table"></table>
    </div>
    <!-- 弹出层，使用的表单 -->
    <div class="layui-row" id="dialog_content" style="display: none">
        <div class="layui-col-md10">
            <form class="layui-form" style="padding-top: 20px;" method="post" action="">
                <div class="layui-form-item">
                    <label class="layui-form-label">消费项目</label>
                    <div class="layui-input-block">
                        <input type="text" name="projectName" lay-verify="required" placeholder="请输入消费项目名称" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">消费类型</label>
                    <div class="layui-input-block">
                        <input type="text" name="type" lay-verify="required" placeholder="请输入消费类型" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">消费金额</label>
                    <div class="layui-input-block">
                        <input type="text" name="price" lay-verify="required|number" placeholder="￥" autocomplete="off" class="layui-input">
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">消费地点</label>
                    <div class="layui-input-block">
                        <input type="text" name="address" lay-verify="required" placeholder="清输入消费地点" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">消费日期</label>
                    <div class="layui-input-block">
                        <input type="text" name="date" id="expenseDate" lay-verify="required|date" name="title" class="layui-input" placeholder="yyyy-mm-dd">
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-input-block" style="text-align: center">
                        <button class="layui-btn" lay-submit lay-filter="dataForm">立即提交</button>
                        <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                    </div>
                </div>
            </form>
        </div>
    </div>
    <script type="text/html" id="table_tool">
        <a href="#" lay-event="edit"><i><span class="layui-icon layui-icon-edit" style="font-size: 22px;"></span></i></a>
        <a href="#" lay-event="del"><i><span class="layui-icon layui-icon-delete" style="font-size: 22px;"></span></i></a>
    </script>
    <script>
        //注意：导航 依赖 element 模块，否则无法进行功能性操作
        layui.use(['element', 'form', 'table', 'laydate', 'layer', 'jquery'], function(){
            var $ = layui.jquery;
            var element = layui.element;
            var table = layui.table;
            var laydate = layui.laydate;
            var form = layui.form;
            var table_options = {
                element: '#tbl_zhichu',
                request_url: '/expense/list',
                table_cols: [[
                    {field: "id", title: "ID", fixed: 'left'},
                    {field: "projectName", title: '消费项目'},
                    {field: "type", title: '消费类型'},
                    {field: "price", title: '消费金额', sort: true},
                    {field: "date", title: '消费日期'},
                    {field: "address", title: '地点'},
                    {fixed: 'right', title: '操 作', width:150, align:'center', toolbar: '#table_tool'}
                ]],
                page: {
                    limit: 10,
                    limits:[10,15,20,25,30]
                }

            };
            laydate.render({
                elem: '#expenseDate'
            })
            table.render({
                elem: table_options.element,
                url: table_options.request_url,
                page: table_options.page,
                cols: table_options.table_cols
            });

            $("#search").click(function() {
                var expenseType = $("#expenseType").val();
                var priceMin = $("#price_min").val();
                var priceMax = $("#price_max").val();
                var where = {
                    expenseType: expenseType,
                    priceMin: priceMin,
                    priceMax: priceMax,
                }
                table.reload('tbl_zhichu', {
                    where: where
                })
            });
            $("#add").click(function() {
                layer.open({
                    type:1,
                    title:"添加消费支出",
                    skin: 'layui-layer-molv',
                    area:['600px', '400px'],
                    content:$("#dialog_content")
                });
            });
            form.on('submit(dataForm)', function (data) {
                $.ajax({
                    url: "/expense/add",
                    type: "post",
                    data: data.field,
                    success:function (res) {
                        if(res.code == 0) {
                            console.log("验证通过，请求code正确，关闭弹层");
                            layer.closeAll('page');
                            table.reload('tbl_zhichu');
                        }
                    }
                });
                return false;
            });
            table.on('tool(table_tool)', function (obj) {
                var id = obj.data.id; //获得当前行数据
                var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
                if(layEvent == "del") {
                    del_data(id);
                }
                if(layEvent == 'edit') {
                    layer.msg("别乱点，这个现在不想做。。。");
                }
            });
            function del_data(id) {
                layer.confirm("Are you sure ?", {
                    btn: ['yes', 'no']
                }, function (index, layero) {
                    $.ajax({
                        url: '/expense/del/' + id,
                        type: 'get',
                        success: function (resp) {
                            if(resp.code == 0){
                                table.reload('tbl_zhichu');
                                layer.closeAll("dialog");
                                layer.msg("删除成功");
                            }
                        }
                    });
                });

            }
        });
    </script>
</body>
</html>